<?php
/* 
 * Global functions to access to common functionality.
 */

/**
 * Return the logged in user
 * @author jmlerma
 * @return User
 */
function get_user(){
    require_once Path::getPath('users').'UserManager.php';
    $manager = new UserManager();

    $user = $manager->getCurrentUser();

    return $user;
}

/**
 * Gets the most problable label for the description receibed as first parameter.
 * The most probable label is calculated from others transactions with the same desc.
 *
 * @author jmlerma
 * @param string $desc
 * @return sting label
 */
function get_label_for_description($desc){
    $db = new ddbb('economy');

    $db->connect(false, true);

    $resource = $db->select('economy', array('description', 'label', 'count(label) as times'),
            "description LIKE '%$desc%' AND label != '' GROUP BY label", array(0,1), 'times DESC');

    $row = $db->fetch($resource);

    if($row && key_exists('label', $row))
        return $row['label'];
    
    return false;
}
?>
